/******************************************************************************
Author: Akshay Dixit
Date Created: October 3, 2018
Data last modified: June 17, 2020
File Name: 4_tz.do
Project: T4D Tanzania 
Purpose: Analysis of intermediate outcomes (Household survey data)
******************************************************************************/

/* LIST OF INTERMEDIATE OUTCOMES & THE ASSOCIATED VARIABLES (Household data)


Category 1. Increased awareness, knowledge and improved community  attitudes

	Outcome 1:	Number of questions on attitudes towards healthcare practices answered appropriately by the respondent. 
				sp01 sp02 sp03
	Outcome 2:	Number of healthcare details that the respondent was able to correctly list.
				pf07a pf09 pf42 
	Outcome 3:	Number of questions on knowledge of health levers that the respondent is able to correctly answer.
				sp06 sp07 sp08 sp09
	Outcome 4:	Number of questions on partner participation/decision making that the respondent is able to answer appropriately.
				sp04 sp05 sp10 pf16
				
Category 2. Improved facility access (transportation, new facility, longer facility hours, outreach services)
				
	Outcome 1:	Transportation
				pf29
	Outcome 2:	Fix road [see .do file titled "section_td"]
				td8a
	Outcome 3:	Travel time
				pf30
	Outcome 4:	Travel cost
				pf31 pf32

Category 5. Increased ability to pay

	Outcome 1:	Address cost of services - Whether the respondent reported cost of care as a barrier to utilization of care.
				pf25
	Outcome 2:	Address cost of services - Total fees (if any) for the delivery reported by the respondent
				pf26
	Outcome 3:	Address cost of services - Whether the respondent (or spouse) has an insurance/health protection program enrolment
				pf26a pf26d
	Outcome 4:	Raise community funds - Whether the respondent was part of a woman's savings group to help save for costs associated with MNH care.
				pk23
	Outcome 5:	Raise community funds - Whether the respondent reported using a community fund to help pay for costs associated with MNH care.
				pk24 pk25

Category 6. Improved attitude, effort, trust of the provider		
	
	Outcome 1:	The level of satisfaction reported by the respondent with the quality of MNH services
				pk07* pk08* pk09* pk10* pk11* pk13* pk14* pk15* pk16* pk17* pk18* pk19* pk20* pk21*
	Outcome 2:	If the respondent reported non-dignified care. 
				pf33 pf34 
	Outcome 3:	If the respondent reported provider neglect.
				pf35 pf36
	Outcome 4:	If the respondent reported provider abuse. 
				pf37
				
Category 9. Improved facility cleanliness
	
	Outcome 1:	Level of cleanliness reported by the respondent
				pk12A pk12B

*/

clear all
set more off

u "$data/T4D_end_hh_clean.dta", clear

********************************************************************************

* CATEGORY 1. Increased awareness, knowledge and improved community  attitudes

***Preparing Outcome 1***
desc sp01 sp02 sp03
foreach var in sp01 sp02 sp03 {
	replace `var' = . if `var' == .d | `var' == .n | `var' == .r
	g `var'_binary = (`var' == 3 | `var' == 4)
	replace `var'_binary = . if `var' == .
	tab `var'
	tab `var'_binary
}

egen knowledge_practices = rowtotal(sp01_binary sp02_binary sp03_binary), missing
replace knowledge_practices = . if sp01_binary == . | sp02_binary == . | sp03_binary == .
	//Include only complete cases in the analysis
tab knowledge_practices


***Preparing Outcome 2***
desc pf07a* pf09* pf42*

	//PF07a
foreach var in pf07a_1 pf07a_2 pf07a_3 pf07a_4 pf07a_5 pf07a_6 pf07a_7 pf07a_8 pf07a_9 pf07a_10 {
	count if `var' == 11 | `var' == 98 | `var' == 95
	tab `var'
	g `var'_binary = (`var' != .)
	replace `var'_binary = 0 if `var' == 11 | `var' == 98 | `var' == 95	
	//"Other", "None" and "Don't Know" replaced as zero. "Other" can be added back in later, as shown below.
	tab `var'_binary
}

split pf07a_other, p(,)

foreach var in pf07a_other1 pf07a_other2 pf07a_other3 pf07a_other4 {
	tab `var'
	g `var'_binary = (`var' != "")
}

egen knowledge_problems = rowtotal(pf07a_1_binary pf07a_2_binary pf07a_3_binary pf07a_4_binary pf07a_5_binary pf07a_6_binary pf07a_7_binary pf07a_8_binary pf07a_9_binary pf07a_10_binary pf07a_other1_binary pf07a_other2_binary pf07a_other3_binary pf07a_other4_binary)
egen knowledge_problems_no_other = rowtotal(pf07a_1_binary pf07a_2_binary pf07a_3_binary pf07a_4_binary pf07a_5_binary pf07a_6_binary pf07a_7_binary pf07a_8_binary pf07a_9_binary pf07a_10_binary)
replace knowledge_problems = . if pf07a_1 == .
replace knowledge_problems_no_other = . if pf07a_1 == .
drop *_binary
tab knowledge_problems
tab knowledge_problems_no_other


	//PF09
foreach var in pf09_1 pf09_2 pf09_3 pf09_4 pf09_5 pf09_6 pf09_7 pf09_8 pf09_9 {
	count if `var' == 98 | `var' == 95
	tab `var'
	g `var'_binary = (`var' != .)
	replace `var'_binary = 0 if `var' == 98 | `var' == 95	//"None", "Other" and "Don't Know" replaced as zero
	tab `var'_binary
}
	
egen knowledge_preparedness = rowtotal(pf09_1_binary pf09_2_binary pf09_3_binary pf09_4_binary pf09_5_binary pf09_6_binary pf09_7_binary pf09_8_binary pf09_9_binary)
replace knowledge_preparedness = . if pf09_1 == .
drop *_binary
tab knowledge_preparedness


	//PF42
foreach var in pf42_1 pf42_2 pf42_3 pf42_4 {
	count if `var' == 97 | `var' == 98 | `var' == 95
	tab `var'
	g `var'_binary = (`var' != .)
	replace `var'_binary = 0 if `var' == 97 | `var' == 98 | `var' == 95	
	//"Other", "None" and "Don't Know" replaced as zero. "Other" can be added back in later, as shown below.
	tab `var'_binary
}

split pf42_other, p(,)

foreach var in pf42_other1 pf42_other2 pf42_other3 pf42_other4 pf42_other5 {
	tab `var'
	g `var'_binary = (`var' != "")
}

egen knowledge_complications = rowtotal(pf42_1_binary pf42_2_binary pf42_3_binary pf42_4_binary pf42_other1_binary pf42_other2_binary pf42_other3_binary pf42_other4_binary pf42_other5_binary)
egen knowledge_complications_no_other = rowtotal(pf42_1_binary pf42_2_binary pf42_3_binary pf42_4_binary)
replace knowledge_complications = . if pf42_1 == .
replace knowledge_complications_no_other = . if pf42_1 == .


***Preparing Outcome 3***
desc sp06* sp07 sp08 sp09

foreach var in sp06_week sp06_month sp06_trimester {
	replace `var' = . if `var' == .d | `var' == .n | `var' == .r
}

g anc_timing = (sp06 == 1 | (sp06_week <= 12 & sp06_week != .) | (sp06_month <= 3 & sp06_month != .) | (sp06_trimester == 1 & sp06_trimester != .))
	//Either "As soon as she knows she is pregnant", OR within 12 weeks, OR within 3 months, OR within the first trimester

replace anc_timing = . if sp06 == . | sp06 == .n | sp06 == .r
replace anc_timing = 0 if sp06 == .d
	//"Don't Know" coded as 0, i.e. a lack of knowledge
	
tab anc_timing

tab sp07
g anc_visits = (sp07 >= 4 & sp07 != .d)
replace anc_visits = 0 if sp07 == .d 
tab anc_visits

tab sp08
g postnatal_yesno = (sp08 == 1)
replace postnatal_yesno = . if sp08 == . | sp08 == .n | sp08 == .r	
replace postnatal_yesno = 0 if sp08 == .d

tab sp09	
count if sp09 == .d
count if sp09 == . | sp09d == .n | sp09d == .r			
g postnatal_timing = (sp09d <= 7 | sp09w <= 1) 	//511 Don't Know responses, all coded as 0
replace postnatal_timing = . if sp09d == .n | sp09d == .r

egen knowledge_levers = rowtotal(anc_timing anc_visits postnatal_yesno postnatal_timing)
replace knowledge_levers = . if anc_timing == . | anc_visits == . | postnatal_yesno == . | postnatal_timing == .
	//Include only complete cases in the analysis
tab knowledge_levers


***Preparing Outcome 4***
desc sp04 sp05 sp10 pf16

tab sp04
g sp04_binary = (sp04 == 1 | sp04 == 2)
replace sp04_binary = . if sp04 == . | sp04 == .d | sp04 == .n | sp04 == .r

tab sp05
g sp05_binary = (sp05 == 3 | sp05 == 4)
replace sp05_binary = . if sp05 == . | sp05 == .d | sp05 == .n | sp05 == .r

tab sp10
g sp10_binary = (sp10 == 1)
replace sp10_binary = . if sp10 == .

tab pf16 
g pf16_binary = (pf16 == 1 | pf16 == 3)
replace pf16_binary = . if pf16 == . | pf16 == .d | pf16 == .n | pf16 == .r

egen partner_participation = rowtotal(sp04_binary sp05_binary sp10_binary pf16_binary)
replace partner_participation = . if sp04_binary == . | sp05_binary == . | sp10_binary == . | pf16_binary == .
	//Include only complete cases in the analysis
tab partner_participation

egen partner_participation_2 = rowtotal(sp04_binary sp05_binary pf16_binary)
replace partner_participation_2 = . if sp04_binary == . | sp05_binary == . | pf16_binary == .
	//Include only complete cases in the analysis

***Results***

gl strata = "strata1 strata2 strata3 strata4 strata5 strata6 strata7"

*Label variables*
lab var knowledge_practices "Knowledge of ANC and facility birth"
lab var knowledge_problems_no_other "Knowledge of pregnancy problems"
lab var knowledge_preparedness "Knowledge of birth preparedness"
lab var knowledge_complications_no_other "Knowledge of postnatal complications"
lab var knowledge_levers "Knowledge of T4D levers"
lab var partner_participation "Attitudes on partner participation"
	
*Produce Excel output
cd "$analysis"

//Create the table shell
putexcel set "intermediate_outcomes.xlsx", sheet("Category 1") modify

putexcel A1=("Outcome") B1=("Treatment Mean") C1=("Control Mean") D1=("Impact") ///
E1=("p-value") F1=("Effect Size") G1=("Sample Size") A9=("Number of Respondents") ///
A10=("Number of villages") B9=(2971) C9=(3037) B10=(100) C10=(100) ///
A12=("Treatment means are regression adjusted") A13=("*** p<0.01, ** p<0.05, * p<0.1")

local outcomes knowledge_practices knowledge_problems_no_other knowledge_preparedness knowledge_complications_no_other knowledge_levers partner_participation
local row = 2
foreach var of local outcomes {
	sleep 2000
	local varlabel : var label `var'
	
	qui reg `var' treatment $strata, vce(cluster facility_id)
	
	local p = 2*ttail(e(df_r), abs(_b[treatment]/_se[treatment]))
	local stars
		if `p' < 0.10 local stars = "*" 
		if `p' < 0.05 local stars = "**" 
		if `p' < 0.01 local stars = "***"
	local impact = _b[treatment]
	
	qui sum `var' if treatment == 0
	putexcel A`row' = ("`varlabel'")
	putexcel B`row' = ((_b[treatment]) + (r(mean)))
	putexcel C`row' = (r(mean))
	putexcel D`row' = ("`impact'" + "`stars'")
	putexcel E`row' = (`p')
	putexcel F`row' = (_b[treatment]/r(sd))
	putexcel G`row' = (e(N))
	local ++row
}


********************************************************************************

* CATEGORY 2. Improved facility access 


***Preparing Outcome 1***
desc pf29*
foreach var in pf29_1 pf29_2 pf29_3 pf29_4 pf29_other {
	tab `var'
}

tab sk09	
tab sk09 if pf29_1 != .	
tab sk09 if pf29_1 == .	//4036 observations for this variable. This makes sense, because
						//data on transportation to the facility is available only for those
						//who gave birth at a facility
g travel_not_on_foot = . 
replace travel_not_on_foot = 0 if (pf29_1 == 1 | pf29_2 == 1 | pf29_3 == 1 | pf29_4 == 1)
replace travel_not_on_foot = 0 if pf29_other == "Walked half the journey on foot and then failed to go on hence she was carried on the bed"
replace travel_not_on_foot = 1 if travel_not_on_foot != 0 & pf29_1 != .
tab travel_not_on_foot

		/* 
		Includes cases where respondents did not travel on foot AT ALL, even in combination
		with other forms of transportation.
		*/
		
g travel_not_on_foot_strict = . 
replace travel_not_on_foot_strict = 0 if (pf29_1 == 1 & pf29_2 == . & pf29_3 == . & pf29_4 == .)
replace travel_not_on_foot_strict = 1 if travel_not_on_foot_strict != 0 & pf29_1 != .
tab travel_not_on_foot_strict
		
		/* 
		The "strict" version includes cases where respondents travelled ONLY on foot.
		*/
		
***Preparing Outcome 3***
desc pf30*
g travel_time = pf30h
replace travel_time = (pf30min/60) if pf30h == . 
tab travel_time
count if travel_time == .d | travel_time == .n | travel_time == .r
count if travel_time == . & pf29_1 != .	//193 cases where the person gave birth at a facility but we don't have a value for travel time


***Preparing Outcome 4***
desc pf31* pf32*
tab pf31
tab pf32
ren pf32 travel_cost
replace travel_cost = 0 if pf31 == 2	//If respondent says they didn't spend on travel, travel cost is 0
tab travel_cost	


***Results***

*Label variables
lab var travel_not_on_foot "Traveled to facility by means other than on foot"
lab var travel_time "Travel time (hours)"
lab var travel_cost "Travel cost (TZS)"
	
*Produce Excel output for policy audiences

//Create the table shell
putexcel set "intermediate_outcomes.xlsx", sheet("Category 2") modify 

putexcel A1=("Outcome") B1=("Treatment Mean") C1=("Control Mean") D1=("Impact") ///
E1=("p-value") F1=("Effect Size") G1=("Sample Size") A7=("Number of Respondents") ///
A8=("Number of villages") B7=(2971) C7=(3037) B8=(100) C8=(100) ///
A10=("Treatment means are regression adjusted") A11=("*** p<0.01, ** p<0.05, * p<0.1")

//Export variable label, regression coefficient, control mean, treatment mean, p-value, sample size
local outcomes travel_not_on_foot travel_time travel_cost 
local row = 2
foreach var of local outcomes {
	sleep 2000
	local varlabel : var label `var'
	
	qui reg `var' treatment $strata, vce(cluster facility_id)
	
	local p = 2*ttail(e(df_r), abs(_b[treatment]/_se[treatment]))
	local stars
		if `p' < 0.10 local stars = "*" 
		if `p' < 0.05 local stars = "**" 
		if `p' < 0.01 local stars = "***"
	local impact = _b[treatment]
	
	qui sum `var' if treatment == 0
	putexcel A`row' = ("`varlabel'")
	putexcel B`row' = ((_b[treatment]) + (r(mean)))
	putexcel C`row' = (r(mean))
	putexcel D`row' = ("`impact'" + "`stars'")
	putexcel E`row' = (`p')
	putexcel F`row' = (_b[treatment]/r(sd))
	putexcel G`row' = (e(N))
	local ++row
}


********************************************************************************

* CATEGORY 5. Increased ability to pay


***Preparing Outcome 1***
foreach var in pf25_1 pf25_2 pf25_3 pf25_4 pf25_5 pf25_other {
	tab `var'
}
	
g cost_barrier = (pf25_1 == 1 | pf25_2 == 1 | pf25_3 == 1 | pf25_4 == 1 | pf25_5 == 1)
	//Respondents who reported cost of care as one reason for not delivering at a facility are coded as "1".
	//Respondents who delivered at a facility are coded as "0". 
tab cost_barrier


***Preparing Outcome 2***
desc *pf26*
tab pf26
tab record_pf26
ren record_pf26 delivery_cost
replace delivery_cost = 0 if pf26 == 2	//If respondent says they didn't spend on delivery, delivery cost is 0
tab delivery_cost
count if delivery_cost == .d 			//57 "don't knows" 
replace delivery_cost = . if delivery_cost == .d | delivery_cost == .n | delivery_cost == .r
g paid_for_delivery = (delivery_cost != 0)
replace paid_for_delivery = . if delivery_cost == .
	

	
***Preparing Outcome 3***
tab pf26a
tab pf26b
tab pf26c
tab pf26d

foreach var in pf26a pf26b pf26c pf26d {
	replace `var' = . if `var' == .d | `var' == .n | `var' == .r
}
	
g insurance_used = (pf26a == 1 | pf26d == 1)
replace insurance_used = . if (pf26a == . & pf26c == .)
g insurance_have = (pf26a == 1 | pf26c == 1)
replace insurance_have = . if (pf26a == . & pf26c == .)

		
***Preparing Outcome 4***
tab pk23
replace pk23 = 0 if pk23 == 2
ren pk23 savings_group_member
tab savings_group_member


***Preparing Outcome 5***
desc pk24 pk25
tab pk24
tab pk25

g have_community_fund = (pk24 == 1)
replace have_community_fund = . if pk24 == . | pk24 == .d | pk24 == .n | pk24 == .r
tab have_community_fund

g used_community_fund = (pk25 == 1)
replace used_community_fund = . if pk24 == . | pk24 == .d | pk24 == .n | pk24 == .r
tab used_community_fund		//If the village does not have a community fund, this variable is 0.


***Results***

*Label variables
lab var cost_barrier "Cost reported as a barrier to healthcare"
lab var delivery_cost "Cost of delivery"
lab var paid_for_delivery "Paid for delivery (binary)"
lab var insurance_used "Insurance enrollment and usage"
lab var savings_group_member "Savings group membership for MNH care"
lab var used_community_fund "Used community fund for MNH care"


*Produce Excel output
cd "$analysis"

//Create the table shell
putexcel set "intermediate_outcomes.xlsx", sheet("Category 5") modify 

putexcel A1=("Outcome") B1=("Treatment Mean") C1=("Control Mean") D1=("Impact") ///
E1=("p-value") F1=("Effect Size") G1=("Sample Size") A11=("Number of Respondents") ///
A12=("Number of villages") B11=(2971) C11=(3037) B12=(100) C12=(100) ///
A14=("Treatment means are regression adjusted") A15=("*** p<0.01, ** p<0.05, * p<0.1")

//Export variable label, regression coefficient, control mean, treatment mean, p-value, sample size
local outcomes cost_barrier delivery_cost paid_for_delivery insurance_used savings_group_member used_community_fund
local row = 2
foreach var of local outcomes {
	sleep 2000
	local varlabel : var label `var'
	
	qui reg `var' treatment $strata, vce(cluster facility_id)
	
	local p = 2*ttail(e(df_r), abs(_b[treatment]/_se[treatment]))
	local stars
		if `p' < 0.10 local stars = "*" 
		if `p' < 0.05 local stars = "**" 
		if `p' < 0.01 local stars = "***"
	local impact = _b[treatment]
	
	qui sum `var' if treatment == 0
	putexcel A`row' = ("`varlabel'")
	putexcel B`row' = ((_b[treatment]) + (r(mean)))
	putexcel C`row' = (r(mean))
	putexcel D`row' = ("`impact'" + "`stars'")
	putexcel E`row' = (`p')
	putexcel F`row' = (_b[treatment]/r(sd))
	putexcel G`row' = (e(N))
	local ++row
}


********************************************************************************

* CATEGORY 6. Improved attitude, effort, trust of the provider

***Preparing Outcome 1***
desc pk01* pk07* pk08* pk09* pk10* pk11* pk12* pk13* pk14* pk15* pk16* pk17* pk18* pk19* pk20* pk21* pk22*

tab pk01	//5449 (90.7%) respondents report visiting a facility in the last 12 months
tab pk03
tab pk22

replace pk22 = . if pk22 == .d | pk22 == .n | pk22 == .r	

	/*
	
	Respondents answered this set of questions for their most recent visit to a facility.
	This most recent visit may or may not have been to a sample dispensary.
	
	Out of the 5449 respondents who visited a facility in the last 12 months:
		1. For 4129, their most recent visit was to the relevant sample dispensary.
		2. 775 respondents did not visit the relevant sample dispensary at all in the last 12 months.
		3. 545 did visit a sample dispensary, but this was not their most recent facility visit.
		
	Two separate sets of outcomes analyzed:
		1. One pertaining to the most recent facility visit (on 5449 observations)
			This includes all respondents who answered this section.
		2. One pertaining only to the sample dispensary (on 4674 observations - 4129+545)
			This includes respondents who visited the sample dispensary in the last 12 months.
	
	*/

local experience1 pk08 pk10 pk11 pk12 pk13 pk14 pk15 pk16 pk17 pk18 pk19 pk20 pk21
foreach var of local experience1 {
	tab `var'
}

local experience2 pk08_d pk10_d pk11_d pk12_d pk13_d pk14_d pk15_d pk16_d pk17_d pk18_d pk19_d pk20_d pk21_d
foreach var of local experience2 {
	tab `var'
}

	//Re-orienting variables so that higher values represent "better" outcomes
		local flip pk08 pk08_d pk10 pk10_d pk12 pk12_d pk14 pk14_d pk15 pk15_d pk16 pk16_d pk17 pk17_d pk18 pk18_d pk19 
		foreach var of local flip {
			lab drop `var'
			tab `var'
			replace `var' = 5 - `var'
			tab `var'
		}
	
		g pk21_new = .
		replace pk21_new = 1 if pk21 == 2
		replace pk21_new = 2 if pk21 == 3
		replace pk21_new = 3 if pk21 == 1
		tab pk21
		tab pk21_new
		
		g pk21_new_d = .
		replace pk21_new_d = 1 if pk21_d == 2
		replace pk21_new_d = 2 if pk21_d == 3
		replace pk21_new_d = 3 if pk21_d == 1
		tab pk21_d
		tab pk21_new_d
		
		
		replace pk20 = 0 if pk20 == 2
		replace pk20_d = 0 if pk20_d == 2
	
local experience3 pk07_1 pk07_2 pk07_3 pk07_4 pk07_5 pk07_6 pk07_d_1 pk07_d_2 pk07_d_3 pk07_d_4 pk07_d_5 pk07_d6 
foreach var of local experience3 {
	tab `var'
}

g greeted_promptly =		(pk07_1 == 1 | pk07_2 == 1 | pk07_3 == 1 | pk07_4 == 1 | pk07_5 == 1 | pk07_6 == 1)
g insufficient_seating =	(pk07_1 == 2 | pk07_2 == 2 | pk07_3 == 2 | pk07_4 == 2 | pk07_5 == 2 | pk07_6 == 2)
g staff_understood_reason =	(pk07_1 == 3 | pk07_2 == 3 | pk07_3 == 3 | pk07_4 == 3 | pk07_5 == 3 | pk07_6 == 3)
g bad_language =			(pk07_1 == 4 | pk07_2 == 4 | pk07_3 == 4 | pk07_4 == 4 | pk07_5 == 4 | pk07_6 == 4)
g properly_informed =		(pk07_1 == 5 | pk07_2 == 5 | pk07_3 == 5 | pk07_4 == 5 | pk07_5 == 5 | pk07_6 == 5)
g not_given_options =		(pk07_1 == 6 | pk07_2 == 6 | pk07_3 == 6 | pk07_4 == 6 | pk07_5 == 6 | pk07_6 == 6)

foreach var in greeted_promptly insufficient_seating staff_understood_reason bad_language properly_informed not_given_options {
	replace `var' = . if pk07_1 == . | pk07_1 == .d | pk07_1 == .n | pk07_1 == .r
	tab `var'
}

g greeted_promptly_d =			(pk07_d_1 == 1 | pk07_d_2 == 1 | pk07_d_3 == 1 | pk07_d_4 == 1 | pk07_d_5 == 1)
g insufficient_seating_d =		(pk07_d_1 == 2 | pk07_d_2 == 2 | pk07_d_3 == 2 | pk07_d_4 == 2 | pk07_d_5 == 2)
g staff_understood_reason_d =	(pk07_d_1 == 3 | pk07_d_2 == 3 | pk07_d_3 == 3 | pk07_d_4 == 3 | pk07_d_5 == 3)
g bad_language_d =				(pk07_d_1 == 4 | pk07_d_2 == 4 | pk07_d_3 == 4 | pk07_d_4 == 4 | pk07_d_5 == 4)
g properly_informed_d =			(pk07_d_1 == 5 | pk07_d_2 == 5 | pk07_d_3 == 5 | pk07_d_4 == 5 | pk07_d_5 == 5)
g not_given_options_d =			(pk07_d_1 == 6 | pk07_d_2 == 6 | pk07_d_3 == 6 | pk07_d_4 == 6 | pk07_d_5 == 6)

foreach var in greeted_promptly_d insufficient_seating_d staff_understood_reason_d bad_language_d properly_informed_d not_given_options_d {
	replace `var' = . if pk07_d_1 == . | pk07_d_1 == .d | pk07_d_1 == .n | pk07_d_1 == .r
	tab `var'
}


local experience4 pk09 pk09h pk09m pk09_d pk09h_d pk09m_d
foreach var of local experience4 {
	tab `var'
}

g pk09_time = .
replace pk09_time = pk09h if pk09 == 1
replace pk09_time = pk09m/60 if pk09 == 2
tab pk09_time

g pk09_time_d = .
replace pk09_time_d = pk09h_d if pk09_d == 1
replace pk09_time_d = pk09m_d/60 if pk09_d == 2
tab pk09_time_d

	*Preparing new variables for analysis pertaining only to the sample dispensary
	
	forvalues i=1/14 {
		local var: word `i' of "pk08" "pk09_time" "pk10" "pk11" "pk12" "pk13" "pk14" "pk15" "pk16" "pk17" "pk18" "pk19" "pk20" "pk21_new"
		g `var'_sample = `var'
		replace `var'_sample = `var'_d if pk22 == 1 & pk22 != .
		replace `var'_sample = . if pk22 == 2 & pk22 != .
		tab `var'
		tab `var'_sample
	}
	
	foreach var in greeted_promptly insufficient_seating staff_understood_reason bad_language properly_informed not_given_options {
		g `var'_sample = `var'
		replace `var'_sample = `var'_d if pk22 == 1 & pk22 != .
		replace `var'_sample = . if pk22 == 2 & pk22 != .
		tab `var'
		tab `var'_sample
	}


	
***Preparing Outcomes 2,3 and 4***

desc pf33* pf34* pf35* pf36* pf37*
local provider_abuse pf33_1 pf33_2 pf33_3 pf34_1 pf34_2 pf34_3 pf35_1 pf35_2 ///
pf35_3 pf36_1 pf36_2 pf36_3 pf37_1 pf37_2 pf37_3
foreach var of local provider_abuse {
	replace `var' = . if `var' == 5
	count if `var' == .d | `var' == .n | `var' == .r
	replace `var' = . if `var' == .d | `var' == .n | `var' == .r
	tab `var'
}

	//Self-experience of provider neglect, abuse or non-dignified care
g non_dignified_care = (pf33_1 == 1 | pf34_1 == 1)
replace non_dignified_care = . if (pf33_1 == . & pf34_1 == .)
tab non_dignified_care

g provider_neglect = (pf35_1 == 1 | pf36_1 == 1)
replace provider_neglect = . if pf35_1 == . & pf36_1 == .
tab provider_neglect

g provider_abuse = (pf37_1 == 1)
replace provider_abuse = . if pf37_1 == .
tab provider_abuse
 
	//Experience/witnessed/heard about provider neglect, abuse or non-dignified care
g non_dignified_care_2 = (pf33_1 == 1 | pf33_1 == 2 | pf33_1 == 3 | pf34_1 == 1 | pf34_1 == 2 | pf34_1 == 3)
replace non_dignified_care_2 = . if (pf33_1 == . & pf34_1 == .)
tab non_dignified_care_2

g provider_neglect_2 = (pf35_1 == 1 | pf35_1 == 2 | pf35_1 == 3 | pf36_1 == 1 | pf36_1 == 2 | pf36_1 == 3)
replace provider_neglect_2 = . if (pf35_1 == . & pf36_1 == .)
tab provider_neglect_2

g provider_abuse_2 = (pf37_1 == 1 | pf37_1 == 2 | pf37_1 == 3)
replace provider_abuse_2 = . if pf37_1 == .
tab provider_abuse_2

***Results***

*Label variables*

lab var pk08 "Recent visit: Satisfaction with waiting time (1-4 scale)"
lab var pk09_time "Recent visit: Longest waiting time (hours)"
lab var pk10 "Recent visit: Satisfaction with opening hours (1-4 scale)"
lab var pk11 "Recent visit: Health worker not there (1-4 scale)"
lab var pk12 "Recent visit: Respectful treatment (1-4 scale)"
lab var pk13 "Recent visit: Ever refused treatment (1-4 scale)"
lab var pk14 "Recent visit: Trust nurses/midwives/other staff (1-4 scale)"
lab var pk15 "Recent visit: Availability of drugs/supplies/equipment (1-4 scale)"
lab var pk16 "Recent visit: Staff communication skills (1-4 scale)"
lab var pk17 "Recent visit: Facility cleanliness (1-4 scale)"
lab var pk18 "Recent visit: Quality of physical facility (1-4 scale)"
lab var pk19 "Recent visit: Overall quality of healthcare (1-4 scale)"
lab var pk20 "Recent visit: If people complained, would staff improve"
lab var pk21_new "Recent visit: Change in the last year (Better care/Same/Worse)"
lab var greeted_promptly "Recent visit: Greeted promptly"
lab var insufficient_seating "Recent visit: Insufficient seating"
lab var staff_understood_reason "Recent visit: Staff understood reason for visit"
lab var bad_language "Recent visit: Staff used bad language"
lab var properly_informed "Recent visit: Properly informed"
lab var not_given_options "Recent visit: Not given options for treatment"

lab var pk08_sample "Sample Dispensary: Satisfaction with waiting time (1-4 scale)"
lab var pk09_time_sample "Sample Dispensary: Longest waiting time (hours)"
lab var pk10_sample "Sample Dispensary: Satisfaction with opening hours (1-4 scale)"
lab var pk11_sample "Sample Dispensary: Health worker not there (1-4 scale)"
lab var pk12_sample "Sample Dispensary: Respectful treatment (1-4 scale)"
lab var pk13_sample "Sample Dispensary: Ever refused treatment (1-4 scale)"
lab var pk14_sample "Sample Dispensary: Trust nurses/midwives/other staff (1-4 scale)"
lab var pk15_sample "Sample Dispensary: Availability of drugs/supplies/equipment (1-4 scale)"
lab var pk16_sample "Sample Dispensary: Staff communication skills (1-4 scale)"
lab var pk17_sample "Sample Dispensary: Facility cleanliness (1-4 scale)"
lab var pk18_sample "Sample Dispensary: Quality of physical facility (1-4 scale)"
lab var pk19_sample "Sample Dispensary: Overall quality of healthcare (1-4 scale)"
lab var pk20_sample "Sample Dispensary: If people complained, would staff improve"
lab var pk21_new_sample "Sample Dispensary: Change in the last year (Better care/Same/Worse)"
lab var greeted_promptly_sample "Sample Dispensary: Greeted promptly"
lab var insufficient_seating_sample "Sample Dispensary: Insufficient seating"
lab var staff_understood_reason_sample "Sample Dispensary: Staff understood reason for visit"
lab var bad_language_sample "Sample Dispensary: Staff used bad language"
lab var properly_informed_sample "Sample Dispensary: Properly informed"
lab var not_given_options_sample "Sample Dispensary: Not given options for treatment"

lab var non_dignified_care "Non-dignified care" 
lab var provider_neglect "Provider neglect"
lab var provider_abuse "Provider abuse"
	
	
*Produce Excel output 
cd "$analysis"

//Create the table shell
putexcel set "intermediate_outcomes.xlsx", sheet("Category 6") modify 

putexcel A1=("Outcome") B1=("Treatment Mean") C1=("Control Mean") D1=("Impact") ///
E1=("p-value") F1=("Effect Size") G1=("Sample Size") A46=("Number of Respondents") ///
A47=("Number of villages") B46=(2971) C46=(3037) B47=(100) C47=(100) ///
A49=("Treatment means are regression adjusted") A50=("*** p<0.01, ** p<0.05, * p<0.1")

//Export variable label, regression coefficient, control mean, treatment mean, p-value, sample size
local outcomes pk08 pk09_time pk10 pk11 pk12 pk13 pk14 pk15 pk16 pk17 pk18 pk19 pk20 pk21_new ///
greeted_promptly insufficient_seating staff_understood_reason bad_language properly_informed not_given_options ///
pk08_sample pk09_time_sample pk10_sample pk11_sample pk12_sample pk13_sample ///
pk14_sample pk15_sample pk16_sample pk17_sample pk18_sample pk19_sample pk20_sample pk21_new_sample ///
greeted_promptly_sample insufficient_seating_sample staff_understood_reason_sample bad_language_sample ///
properly_informed_sample not_given_options_sample ///
non_dignified_care provider_neglect provider_abuse
local row = 2
foreach var of local outcomes {
	sleep 2000
	local varlabel : var label `var'
	
	qui reg `var' treatment $strata, vce(cluster facility_id)
	
	local p = 2*ttail(e(df_r), abs(_b[treatment]/_se[treatment]))
	local stars
		if `p' < 0.10 local stars = "*" 
		if `p' < 0.05 local stars = "**" 
		if `p' < 0.01 local stars = "***"
	local impact = _b[treatment]
	
	qui sum `var' if treatment == 0
	putexcel A`row' = ("`varlabel'")
	putexcel B`row' = ((_b[treatment]) + (r(mean)))
	putexcel C`row' = (r(mean))
	putexcel D`row' = ("`impact'" + "`stars'")
	putexcel E`row' = (`p')
	putexcel F`row' = (_b[treatment]/r(sd))
	putexcel G`row' = (e(N))
	local ++row
}


********************************************************************************

* CATEGORY 9. Improved facility cleanliness

/*

This is an analysis of question PK17, which is done above along with all the other Category 6
variables.

The analysis of the PK17 variables (pk17 and pk17_d) was manually cut-and-paste to a different Excel sheet, 
to avoid duplicating this part of the code. 

*/

********************************************************************************

clear



